package com.easemob.helpdesk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.housekeeperapp.R;

import org.w3c.dom.Text;

/**
 * 自定义AlerDialog
 */
public class AlertDialog extends BaseActivityV2 implements OnClickListener {
	/**
	 * Dialog 显示信息View
	 */
	private TextView tvMessage;
	private LinearLayout dialog_container;

	private TextView tvTitle;
	private View tvTitleLine;
	/**
	 * Dialog 确定，取消按钮
	 */
	private Button btnOk,btnCancel;
	/**
	 * 临时记录配置变量
	 */
	private int position;
	/**
	 * 文本框显示内容
	 */
	private String textMessage;

	private String textTitle;

	//临时存储order id
	private long order_id;
	/**
	 * 确定按钮，显示文字
	 */
	private String okString = null;

	private String cancelString = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_custom_view);
		Intent gIntent = getIntent();
		textMessage = gIntent.getStringExtra("msg");
		okString = gIntent.getStringExtra("okString");
		textTitle = gIntent.getStringExtra("title");
		cancelString = gIntent.getStringExtra("cancelString");

		position = gIntent.getIntExtra("position", -1);

		order_id = gIntent.getLongExtra("order_id", 0);
		initView();
		
	}

	/**
	 * 获取当前Dialog内的View
	 */
	private void initView() {
		tvMessage = (TextView) findViewById(R.id.dialog_message);
		tvTitle = (TextView) findViewById(R.id.dialog_title);
		tvTitleLine = findViewById(R.id.dialog_title_line);
		dialog_container = (LinearLayout) findViewById(R.id.dialog_container);

		btnOk = (Button) findViewById(R.id.ok);
		btnCancel = (Button) findViewById(R.id.cancel);

		int outerwidth = getResources().getDisplayMetrics().widthPixels;
		ViewGroup.LayoutParams layoutlp = dialog_container.getLayoutParams();
		layoutlp.width = outerwidth - 85;
		dialog_container.setLayoutParams(layoutlp);


		btnCancel.setOnClickListener(this);
		btnOk.setOnClickListener(this);
		if(!TextUtils.isEmpty(textMessage)){
			tvMessage.setText(Html.fromHtml(textMessage));
		}
		if(!TextUtils.isEmpty(okString)){
			btnOk.setText(okString);
		}

		if(!TextUtils.isEmpty(textTitle)){
			tvTitle.setVisibility(View.VISIBLE);
			tvTitleLine.setVisibility(View.VISIBLE);
			tvTitle.setText(textTitle);
		}

		if(!TextUtils.isEmpty(cancelString)){
			btnCancel.setText(cancelString);
		}
		
	}

	@Override
	public void onClick(View v) {

		if(v.getId()==R.id.ok){
			Intent intent = getIntent().putExtra("position", position);
			if(order_id != 0){
				intent.putExtra("order_id", order_id);
			}

			setResult(RESULT_OK, intent);
			if(position != -1){
				ChatActivity.resendPos = position;
			}
			finish();
		} else if (v.getId()==R.id.cancel) {
			setResult(RESULT_CANCELED, getIntent().putExtra("position", position));
			finish();
		}
//		switch (v.getId()) {
//		case R.id.ok:
//
//			break;
//		case R.id.cancel:
//			setResult(RESULT_CANCELED, getIntent().putExtra("position", position));
//			finish();
//			break;
//		default:
//			break;
//		}
		
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		Intent gIntent = getIntent();
		textMessage = gIntent.getStringExtra("msg");
		okString = gIntent.getStringExtra("okString");
		position = gIntent.getIntExtra("position", -1);
		initView();
	}
}
